package eu.ydp.mathplayer.editor.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ToggleButton;

import eu.ydp.mathplayer.editor.controller.ActionSocket;
import eu.ydp.mathplayer.editor.controller.InsertTokenType;

public class MenuView extends Composite {

	private Panel container;
	private Panel menuPanel;
	private Panel submenuPanel;
	private Panel toolsPanel;
	
	private Panel layoutsPanel;
	private Panel operatorsPanel;
	private Panel relationsPanel;
	private Panel bracketsPanel;
	private Panel functionsPanel;
	private Panel symbolsPanel;
	private Panel greeksmallsPanel;
	private Panel greekcapitalsPanel;

	private ToggleButton layoutsButton;
	private ToggleButton operatorsButton;
	private ToggleButton relationsButton;
	private ToggleButton bracketsButton;
	private ToggleButton functionsButton;
	private ToggleButton symbolsButton;
	private ToggleButton greeksmallsButton;
	private ToggleButton greekcapitalsButton;

	private Button fractionButton;
	private Button subButton; 
	private Button supButton; 
	private Button subsupButton; 
	private Button underButton; 
	private Button overButton; 
	private Button underoverButton; 
	private Button rootButton;
	private Button sqrtButton;
	
	private Button fencedRoundButton;
	private Button fencedSquareButton;
	private Button fencedCurlyButton;
	private Button fencedAngleButton;
	
	private Button ltButton; 
	
	private ActionSocket actionSocket;
	
	public MenuView(){
		
		container = new FlowPanel();
		container.setStyleName("math-editor-menu-container");
		
		initWidget(container);
		
	}
	
	public void init(ActionSocket actionSocket){
		this.actionSocket = actionSocket;
		final ActionSocket acSock = actionSocket;
		
		menuPanel = new FlowPanel();
		menuPanel.setStyleName("math-editor-mainmenu");
		container.add(menuPanel);

		submenuPanel = new FlowPanel();
		submenuPanel.setStyleName("math-editor-submenu");
		container.add(submenuPanel);

		toolsPanel = new FlowPanel();
		toolsPanel.setStyleName("math-editor-tools");
		container.add(toolsPanel);

		createAndInsertActionButton("tool", "+", toolsPanel, new ClickHandler() {
			public void onClick(ClickEvent event) {
				acSock.zoom(true);
			}
		});
		createAndInsertActionButton("tool", "-", toolsPanel, new ClickHandler() {
			public void onClick(ClickEvent event) {
				acSock.zoom(false);
			}
		});
		

		layoutsPanel = new FlowPanel();
		layoutsPanel.setStyleName("math-editor-submenu-layouts");
		operatorsPanel = new FlowPanel();
		operatorsPanel.setStyleName("math-editor-submenu-operators");
		relationsPanel = new FlowPanel();
		relationsPanel.setStyleName("math-editor-submenu-relations");
		bracketsPanel = new FlowPanel();
		bracketsPanel.setStyleName("math-editor-submenu-brackets");
		functionsPanel = new FlowPanel();
		functionsPanel.setStyleName("math-editor-submenu-functions");
		symbolsPanel = new FlowPanel();
		symbolsPanel.setStyleName("math-editor-submenu-symbols");
		greeksmallsPanel = new FlowPanel();
		greeksmallsPanel.setStyleName("math-editor-submenu-greeksmalls");
		greekcapitalsPanel = new FlowPanel();
		greekcapitalsPanel.setStyleName("math-editor-submenu-greekcapitals");
		
		
		layoutsButton = createAndInsertMainmenuButton("layouts",layoutsPanel);
		operatorsButton = createAndInsertMainmenuButton("operators",operatorsPanel);
		relationsButton = createAndInsertMainmenuButton("relations",relationsPanel);
		bracketsButton = createAndInsertMainmenuButton("brackets",bracketsPanel);
		functionsButton = createAndInsertMainmenuButton("functions",functionsPanel);
		symbolsButton = createAndInsertMainmenuButton("symbols",symbolsPanel);
		greeksmallsButton = createAndInsertMainmenuButton("greeksmalls",greeksmallsPanel);
		greekcapitalsButton = createAndInsertMainmenuButton("greekcapitals",greekcapitalsPanel);
		
		
		fractionButton = createAndInsertSubmenuButton("fraction", InsertTokenType.FRACTION, layoutsPanel);
		subsupButton = createAndInsertSubmenuButton("subsup", InsertTokenType.SUBSUP, layoutsPanel);
		subButton = createAndInsertSubmenuButton("sub", InsertTokenType.SUB, layoutsPanel);
		supButton = createAndInsertSubmenuButton("sup", InsertTokenType.SUP, layoutsPanel);
		underoverButton = createAndInsertSubmenuButton("underover", InsertTokenType.UNDEROVER, layoutsPanel);
		underButton = createAndInsertSubmenuButton("under", InsertTokenType.UNDER, layoutsPanel);
		overButton = createAndInsertSubmenuButton("over", InsertTokenType.OVER, layoutsPanel);
		sqrtButton = createAndInsertSubmenuButton("sqrt", InsertTokenType.SQRT, layoutsPanel);
		rootButton = createAndInsertSubmenuButton("root", InsertTokenType.ROOT, layoutsPanel);

		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 43);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 8722);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 215);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 183);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 58);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 8260);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 247);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 177);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 8869);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 8741);
		createAndInsertSubmenuButtonWithText("operator", InsertTokenType.OPERATOR, operatorsPanel, 8742);

		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 61);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8800);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 60);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 62);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8804);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8805);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8801);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8802);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8776);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8773);
		createAndInsertSubmenuButtonWithText("relation", InsertTokenType.OPERATOR, relationsPanel, 8774);

		fencedRoundButton = createAndInsertSubmenuButton("fenced-round", InsertTokenType.FENCE_ROUND, bracketsPanel);
		fencedSquareButton = createAndInsertSubmenuButton("fenced-square", InsertTokenType.FENCE_SQUARE, bracketsPanel);
		fencedCurlyButton = createAndInsertSubmenuButton("fenced-curly", InsertTokenType.FENCE_CURLY, bracketsPanel);
		fencedAngleButton = createAndInsertSubmenuButton("fenced-angle", InsertTokenType.FENCE_ANGLE, bracketsPanel);
		fencedAngleButton = createAndInsertSubmenuButton("fenced-round-square", InsertTokenType.FENCE_ROUND_SQUARE, bracketsPanel);
		fencedAngleButton = createAndInsertSubmenuButton("fenced-square-round", InsertTokenType.FENCE_SQUARE_ROUND, bracketsPanel);
		fencedAngleButton = createAndInsertSubmenuButton("fenced-round-angle", InsertTokenType.FENCE_ROUND_ANGLE, bracketsPanel);
		fencedAngleButton = createAndInsertSubmenuButton("fenced-angle-round", InsertTokenType.FENCE_ANGLE_ROUND, bracketsPanel);

		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN, functionsPanel, "sin x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN, functionsPanel, "cos x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN, functionsPanel, "tan x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN, functionsPanel, "ctg x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN_P, functionsPanel, "sin&#185; x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN_P, functionsPanel, "cos&#185; x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN_P, functionsPanel, "tan&#185; x");
		createAndInsertSubmenuButtonWithText("function", InsertTokenType.SIN_P, functionsPanel, "ctg&#185; x");

		createAndInsertSubmenuButtonWithText("symbol", InsertTokenType.IDENTIFIER, symbolsPanel, 8734);
		createAndInsertSubmenuButtonWithText("symbol", InsertTokenType.IDENTIFIER, symbolsPanel, 9651);

		for (Integer ch = 945 ; ch <= 969 ; ch ++){
			createAndInsertSubmenuButtonWithText("greeksmall", InsertTokenType.IDENTIFIER, greeksmallsPanel, ch);
		}
		for (Integer ch = 913 ; ch <= 937 ; ch ++){
			createAndInsertSubmenuButtonWithText("greekcapital", InsertTokenType.IDENTIFIER, greekcapitalsPanel, ch);
		}
		
		toggleButton(layoutsButton);
		showPanel(layoutsPanel);
	}

	private ToggleButton createAndInsertMainmenuButton(String name, final Panel panelToShow){
		final ToggleButton btn = new ToggleButton();
		btn.setHTML("<div class=\"math-editor-mainmenu-button-face\"></div>");
		btn.addStyleName("math-editor-button-" + name);
		btn.addStyleName("math-editor-mainmenu-button");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				showPanel(panelToShow);
				toggleButton(btn);
			}
		});
		menuPanel.add(btn);
		return btn;
	}

	private Button createAndInsertSubmenuButton(String name, final InsertTokenType tokenToInsert, Panel owner){
		return createAndInsertSubmenuButton(name, tokenToInsert, owner, new Object[0]);
	}
	
	private Button createAndInsertSubmenuButton(String name, final InsertTokenType tokenToInsert, Panel owner, final Object[] args){
		final ActionSocket acSock = actionSocket;
		Button btn = new Button("<div class=\"math-editor-submenu-button-face\"></div>");
		btn.addStyleName("math-editor-button-" + name);
		btn.addStyleName("math-editor-submenu-button");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				acSock.insertToken(tokenToInsert, args);
			}
		});
		owner.add(btn);
		return btn;
	}
	
	private Button createAndInsertSubmenuButtonWithText(String name, final InsertTokenType tokenToInsert, Panel owner, final Integer charCode){
		final ActionSocket acSock = actionSocket;
		Button btn = new Button("<div class=\"math-editor-submenu-button-face\">&#"+charCode.toString()+";</div>");
		btn.addStyleName("math-editor-button-" + name);
		btn.addStyleName("math-editor-submenu-button");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				acSock.insertToken(tokenToInsert, new Object[]{   String.valueOf(((char)(int)charCode))   });
			}
		});
		owner.add(btn);
		return btn;
	}
	
	private Button createAndInsertSubmenuButtonWithText(String name, final InsertTokenType tokenToInsert, Panel owner, final String text){
		final ActionSocket acSock = actionSocket;
		Button btn = new Button("<div class=\"math-editor-submenu-button-face\">"+text+"</div>");
		btn.addStyleName("math-editor-button-" + name);
		btn.addStyleName("math-editor-submenu-button");
		btn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				acSock.insertToken(tokenToInsert, new Object[]{text.substring(0, 3)});
			}
		});
		owner.add(btn);
		return btn;
	}
	
	private void createAndInsertActionButton(String name, String text, Panel owner, ClickHandler action){
		Button btn = new Button("<div class=\"math-editor-submenu-button-face\">"+text+"</div>");
		btn.addStyleName("math-editor-button-" + name);
		btn.addStyleName("math-editor-tools-button");
		btn.addClickHandler(action);
		owner.add(btn);
	}
	
	private void toggleButton(ToggleButton btn){
		layoutsButton.setDown(false);
		operatorsButton.setDown(false);
		relationsButton.setDown(false);
		bracketsButton.setDown(false);
		functionsButton.setDown(false);
		symbolsButton.setDown(false);
		greeksmallsButton.setDown(false);
		greekcapitalsButton.setDown(false);
		
		btn.setDown(true);
	}
	
	private void showPanel(Panel panel){
		submenuPanel.clear();
		
		submenuPanel.add(panel);
	}
}
